﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Ionic.Zip;

namespace METU_CMT.Business.Compression
{
    class DeliverableTeamName
    {
        public Deliverable Deliverable { get; set; }

        public string TeamName { get; set; }
    }


    public class CompressionAdapter
    {
        private List<DeliverableTeamName> deliverables;
        private List<Note> lectureNotes;
        private ZipFile _zip;
        public CompressionAdapter()
        {
            deliverables = new List< DeliverableTeamName >();
            lectureNotes = new List<Note>();
            _zip = new ZipFile();
        }

        public void AddFile( Deliverable file, string teamName )
        {
            deliverables.Add( new DeliverableTeamName {Deliverable = file, TeamName = teamName} );
        }

        public void AddFile(Note lectureNote)
        {

        }

        public void CompressAndDownloadDeliverables()
        {
            DateTime _now = DateTime.Now;
            foreach (DeliverableTeamName _deliverableTeamName in deliverables)
            {
                if (_deliverableTeamName.Deliverable != null)
                {                        
                    if (File.Exists(HttpContext.Current.Server.MapPath("~/StudentDocuments/" + _deliverableTeamName.Deliverable.File)) == true)
                    {
                        _zip.AddFile(
                            HttpContext.Current.Server.MapPath( "~/StudentDocuments/" +
                                                                _deliverableTeamName.Deliverable.File ),
                            _deliverableTeamName.Deliverable.Event.Description + "/" + _deliverableTeamName.TeamName );
                    }
                }
            }
            _zip.Save(
                HttpContext.Current.Server.MapPath("~/StudentDocuments/") + "Deliverables$" +
                                                   _now.ToString("yyyy.MM.dd HH.mm.ss") + ".zip");

            CommonMethods.DownloadFile(HttpContext.Current.Response,
                                       HttpContext.Current.Server.MapPath("~/StudentDocuments/") + "Deliverables$" +
                                                                          _now.ToString("yyyy.MM.dd HH.mm.ss") + ".zip",
                                       FileType.Zip);
        }

        public void CompressAndDownloadLectureNotes()
        {

        }


    }
}